package com.zb.user.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjUserService;
import com.zb.user.service.ConUserService;
import com.zb.utils.TokenUtil;
import com.zb.vo.UserSelectVo;
import com.zb.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-08-08
 */
@Api(tags = "用户类接口")
@RestController
@RequestMapping("/user")
public class ZjUserController {
    @Resource
    private ZjUserService userService;
    @Resource
    private ConUserService conUserService;

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, remark = "用户档案 - 新增或修改用户")
    @ApiOperation("新增或修改用户")
    @PostMapping("/add")
    public Result save(HttpServletRequest request, @RequestBody @Valid UserVo userVo) {
        AccounLoginInfo accountLoginInfo = TokenUtil.getAccountLoginInfo(request);
        userService.save(accountLoginInfo, userVo);
        return Result.success(null == userVo.getId()? "新增用户成功" : "修改用户成功");
    }

    @ApiOperation("获取用户列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody @Valid UserSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取用户列表成功！").data(conUserService.getList(custAccId, selectVo));
    }

    @ApiOperation("获取用户账号信息")
    @GetMapping("/{id}")
    public Result getUserAccount(@PathVariable @ApiParam(value = "用户ID", required = true) Long id) {
        return Result.success("获取用户账号信息成功").data("row", userService.getUserAccount(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "用户档案 - 批量更改用户状态")
    @ApiOperation("批量更改用户状态")
    @PutMapping("/status")
    public Result status(@RequestParam @ApiParam(value = "用户状态：0-正常，1-冻结，2-禁用", required = true, example = "0")  Integer status,
                         @RequestBody @ApiParam(value = "用户ID集合", required = true) List<Long> idList) {
        Assert.isTrue(Pattern.matches("^(0|1|2)$", status.toString()), "用户状态参数不合法！");
        userService.updateBatchStatusByIds(idList, status);
        return Result.success(Objects.equals(0, status)? "启用用户成功" : Objects.equals(1, status)? "冻结用户成功" : "禁用成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.DELETE, remark = "用户档案 - 批量删除用户")
    @ApiOperation("批量删除用户")
    @DeleteMapping("/")
    public Result delete(@RequestBody @ApiParam(value = "用户ID集合", required = true) List<Long> idList) {
        conUserService.deleteBatchByIds(idList);
        return Result.success("删除成功！");
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "用户档案 - 更新客户知识库查看权限")
    @ApiOperation("更新客户知识库查看权限")
    @PutMapping("/updateSelectKnowledgeAuthor/{id}")
    public Result updateSelectKnowledgeAuthor(@PathVariable @ApiParam(value = "客户ID", required = true) Long id,
                                              @RequestParam @ApiParam(value = "知识库查看权限：false-查看所有，true-根据客户分类查看") boolean selectKnowledgeAuthor) {
        userService.updateSelectKnowledgeAuthor(id, selectKnowledgeAuthor);
        return Result.success("知识库查看权限更新成功");
    }

    @ApiOperation("获取客户关联的工单数量")
    @GetMapping("/getUserRelationOrderQty/{userId}")
    public Result getUserRelationOrderQty(@PathVariable @ApiParam(value = "客户ID", required = true) Long userId) {
        return Result.success("获取客户关联的工单数量成功").data(PageUtil.row(conUserService.getUserRelationOrderQty(userId)));
    }

    @ApiOperation("获取客户关联的工单列表")
    @PostMapping("/getUserRelationOrderList/{userId}")
    public Result getUserRelationOrderList(@PathVariable @ApiParam(value = "客户ID", required = true) Long userId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取客户关联的工单列表成功").data(conUserService.getUserOrderList(userId, pageUtil));
    }

      @ApiOperation("获取客户关联的工单列表是否重复工单")
    @PostMapping("/getUserRelationRepeatOrderList/{userId}")
    public Result getUserRelationRepeatOrderList(@PathVariable @ApiParam(value = "客户ID", required = true) Long userId) {
        return Result.success("获取客户关联的工单列表是否重复工单成功").data(conUserService.getUserOrderListWithoutPagination(userId));
    }

    @ApiOperation("获取客户关联的编码商品数量")
    @GetMapping("/getUserRelationCodeGoodsQty/{userId}")
    public Result getUserRelationCodeGoodsQty(@PathVariable @ApiParam(value = "客户ID", required = true) Long userId) {
        return Result.success().data(PageUtil.row(conUserService.getUserRelationCodeGoodsQty(userId)));
    }

    @ApiOperation("获取客户关联的编码商品列表")
    @PostMapping("/getUserRelationCodeGoodsList/{userId}")
    public Result getUserRelationCodeGoodsList(@PathVariable @ApiParam(value = "客户ID", required = true) Long userId, @RequestBody PageUtil pageUtil) {
        return Result.success().data(conUserService.getUserRelationCodeGoodsList(userId, pageUtil));
    }

    @ApiOperation("根据工单ID获取客户关联信息")
    @GetMapping("/getUserRelationInfo/{workId}")
    public Result getUserRelationInfo(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success("获取客户关联信息成功").data(PageUtil.row(conUserService.getUserRelationInfo(workId)));
    }






}
